//
//  PresentedViewController.m
//  AnimationDemo
//
//  Created by jiaozenglian on 2020/7/12.
//  Copyright © 2020 Jiaozl. All rights reserved.
//

#import "PresentedViewController.h"
#import "SpringAnimationTransition.h"
#import "ZLTransitionDelegate.h"
#import "ZLInteractiveTransition.h"
@interface PresentedViewController ()
@property (nonatomic, strong) ZLTransitionDelegate *transitionDelegate;
@property (nonatomic, strong) SpringAnimationTransition *springAnimationTransition;
@end

@implementation PresentedViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.modalPresentationStyle = UIModalPresentationCustom;
        self.transitioningDelegate = self.transitionDelegate;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setupGesture];
    self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, 400.0);
}

- (void)setupGesture
{
    // 添加屏幕边缘滑动手势
    UIPanGestureRecognizer *screenEdgePanGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(_actionLeftSwipe:)];
    [self.view addGestureRecognizer:screenEdgePanGR];
}

- (void)_actionLeftSwipe:(UIScreenEdgePanGestureRecognizer *)sender
{
    [self _actionPresentNewVC:sender];
}

- (void)_actionPresentNewVC:(id)sender
{
    if ([sender isKindOfClass:UIGestureRecognizer.class] ) {
        if (((UIGestureRecognizer *)sender).state == UIGestureRecognizerStateBegan) {
            [self _actionDismiss:sender];
        }
    } else {
        [self _actionDismiss:sender];
    }
}

- (IBAction)_actionDismiss:(id)sender
{
    if ([sender isKindOfClass:UIGestureRecognizer.class] ) {
        self.gestureRecognizer = sender;
    } else {
        self.gestureRecognizer = nil;
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

// MARK: - getter
- (ZLTransitionDelegate *)transitionDelegate
{
    if (!_transitionDelegate) {
        _transitionDelegate = [[ZLTransitionDelegate alloc] init];
        weakify(self);
        _transitionDelegate.animatedPresentTransition = ^id<UIViewControllerAnimatedTransitioning> _Nonnull{
            strongify(self);
            return self.springAnimationTransition;
        };
        _transitionDelegate.animatedDismissalTransition = ^id<UIViewControllerAnimatedTransitioning> _Nonnull{
            strongify(self);
            return self.springAnimationTransition;
        };
        _transitionDelegate.interactivePresentTransition = ^id<UIViewControllerInteractiveTransitioning> _Nonnull{
            strongify(self);
            if (self.gestureRecognizer) {
                return [[ZLInteractiveTransition alloc] initWithGestureRecognizer:self.gestureRecognizer edgeForDragging:UIRectEdgeBottom];
            }
            return nil;
        };
        _transitionDelegate.interactiveDismissalTransition = ^id<UIViewControllerInteractiveTransitioning> _Nonnull{
            strongify(self);
            if (self.gestureRecognizer) {
                return [[ZLInteractiveTransition alloc] initWithGestureRecognizer:self.gestureRecognizer edgeForDragging:UIRectEdgeTop];
            }
            return nil;
        };
    }
    return _transitionDelegate;
}
- (SpringAnimationTransition *)springAnimationTransition
{
    if (!_springAnimationTransition) {
        _springAnimationTransition = [SpringAnimationTransition animatedTransition];
    }
    return _springAnimationTransition;
}

@end
